<?php
// @formatter:off
/**
 * @file WCTableRepresentation.abs.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-04-30 02:04:27 +0000 (Tue, 30 Apr 2013) $
 *
 * $Id: WCTableRepresentation.abs.php 63 2013-04-30 02:04:27Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/representations/WCTableRepresentation.abs.php $
 */
// @formatter:on

/**
 * @abstract
 * @class WCTableRepresentation
 */
abstract class WCTableRepresentation extends WCRepresentation {
	//
	// Public Methods
	public function load($id) {
		$this->_ok = false;
		$this->_info = array("{$this->_PropertiesPrefix}id" => $id);

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[".$this->_PropertiesTable."]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;

			$query = "select  *\n";
			$query .= "from    {$table}\n";
			$query .= "where   {$prefix}id = :id\n";
			$stmt = $this->_db->prepare($stmtId, $query);
		}

		$params = array(":id" => $this->id);

		$result = $stmt->execute($params);

		if($result && $stmt->rowCount() == 1) {
			$this->_info = $stmt->fetch();
			$this->_ok = true;

			foreach($this->_PropertiesBoolean as $property) {
				$prop = $this->_PropertiesPrefix.$property;
				$this->_info[$prop] = isset($this->_info[$prop]) ? $this->_info[$prop] == "Y" : false;
			}
		} elseif(!$result) {
			global $wcLog;

			$error = $stmt->errorInfo();
			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load id '{$id}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Error: [{$error[1]}] {$error[2]}";
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			trigger_error($msg, E_USER_WARNING);
		} elseif($stmt->rowCount() != 1) {
			global $wcLog;

			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load id '{$id}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Resulting rows count: ".$stmt->rowCount();
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			trigger_error($msg, E_USER_WARNING);
		}

		return $this->ok();
	}
	public function loadByName($name) {
		$this->_ok = false;
		$this->_info = array();

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[".$this->_PropertiesTable."]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;
			$cname = $this->_PropertiesNameColumn;

			$query = "select  {$prefix}id as id,\n";
			$query .= "        {$prefix}{$cname} as name\n";
			$query .= "from    {$table}\n";
			$query .= "where   {$prefix}{$cname} = :name\n";
			$stmt = $this->_db->prepare($stmtId, $query);
		}

		$params = array(":name" => $name);

		$result = $stmt->execute($params);

		if($result && $stmt->rowCount() == 1) {
			$row = $stmt->fetch();
			//
			// $name == $row["name"]:
			// name is checked only to ensure case sensitive match.
			if($name == $row["name"]) {
				$this->load($row["id"]);
			}
		} elseif(!$result) {
			global $wcLog;

			$error = $stmt->errorInfo();
			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load {$this->_PropertiesNameColumn} '{$name}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Error: [{$error[1]}] {$error[2]}";
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			trigger_error($msg, E_USER_WARNING);
		} elseif($stmt->rowCount() != 1) {
			global $wcLog;

			$msg = __CLASS__."::".__FUNCTION__."(): There was an error trying to load {$this->_PropertiesNameColumn} '{$name}' from table '{$this->_dbprefix}{$this->_PropertiesTable}'.";
			$msg .= " Resulting rows count: ".$stmt->rowCount();
			if($wcLog) {
				$wcLog->write($msg);
				///@todo check level;
			}
			trigger_error($msg, E_USER_WARNING);
		}

		return $this->ok();
	}
	public function reload() {
		return $this->load($this->id);
	}
	//
	// Protected Methods
}
?>